package com.hzmg.support.uri;

import akka.http.javadsl.model.Uri;
import cn.hutool.core.lang.Assert;

/**
 * 构建 URI
 * 构建方法不完善，后续补充
 *
 * @author Yukoo
 */
public class UriBuilder {

    private String scheme;

    private String userInfo;

    private String host;

    private Integer port;

    private final String COLON = ":";

    private final String SCHEME_SUFFIX = COLON + "//";

    public static UriBuilder setBuilder() {
        UriBuilder uriBuilder = new UriBuilder();
        return uriBuilder;
    }

    public UriBuilder scheme(String scheme) {
        this.scheme = scheme;
        return this;
    }

    public UriBuilder userInfo(String userInfo) {
        this.userInfo = userInfo;
        return this;
    }

    public UriBuilder host(String host) {
        this.host = host;
        return this;
    }

    public UriBuilder port(int port) {
        this.port = port;
        return this;
    }

    public Uri build() {
        String stringUri = jointUri(this.scheme, this.userInfo, this.host, this.port);
        Uri uri = Uri.create(stringUri);

        return uri;
    }


    private String jointUri(String secheme, String userInfo, String host, Integer port) {
        Assert.notEmpty(secheme, "secheme can not be null");
        Assert.notEmpty(host, "host can not be null");

        StringBuilder sb = new StringBuilder();
        sb.append(secheme + SCHEME_SUFFIX);
        sb.append(host);

        if (port != null) {
            sb.append(COLON + port);
        }
        //TODO: userInfo

        return sb.toString();
    }
}
